package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.pojo.RecommendUser;
import com.tanhua.dubbo.pojo.UserInfo;
import com.tanhua.dubbo.vo.PageInfo;
import com.tanhua.server.config.DefaultConfig;
import com.tanhua.server.config.AliyunDefaultUserConfig;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class TodayBestService {

    @Autowired
    private UserService userService;

    @DubboReference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;

    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;

    @Autowired
    private AliyunDefaultUserConfig aliyunDefaultUserConfig;

    @Autowired
    private DefaultConfig defaultConfig;
    //@Value("${tanhua.default.user}")
    //private Long defaultUser; //TODO 默认推荐用户，这里建议写到nacos中(完成)

    //@Value("${tanhua.default.users}")
    //private List<Long> defaultUsers; //TODO 默认推荐用户，这里建议写到nacos中（完成）

    public Object queryTodayBest() {
        Long userId = UserThreadLocal.get();

        //查询得分最高的人
        RecommendUser recommendUser = this.recommendUserApi.queryWithMaxScore(userId);

       // recommendUser =null;
        if (null == recommendUser) {
            //给出一个默认的推荐用户
            recommendUser = new RecommendUser();
            recommendUser.setUserId(this.aliyunDefaultUserConfig.getUser());
            recommendUser.setUserId(this.defaultConfig.getUser());
            recommendUser.setToUserId(userId);
            recommendUser.setScore(RandomUtil.randomDouble(80, 99));
        }

        //查询用户信息，封装成TodayBest对象返回
        UserInfo userInfo = this.userInfoApi.queryByUserId(recommendUser.getUserId());
        TodayBest todayBest =  BeanUtil.toBeanIgnoreError(userInfo, TodayBest.class);

            todayBest.setFateValue(recommendUser.getScore().longValue());

            todayBest.setGender(userInfo.getSex().name().toLowerCase());



        return todayBest;
    }

    public PageResult queryRecommendation(RecommendUserQueryParam queryParam) {
        //校验token是否有效
        Long userId = UserThreadLocal.get();
        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());

        //通过dubbo服务查询推荐列表
        PageInfo<RecommendUser> pageInfo = this.recommendUserApi.queryPageInfo(userId, queryParam.getPage(), queryParam.getPagesize());
        List<RecommendUser> recommendUserList = pageInfo.getRecords();
        if (CollUtil.isEmpty(recommendUserList)) {
            recommendUserList = new ArrayList<>();
            //给出默认的推荐列表
            for (Long uid : this.aliyunDefaultUserConfig.getUsers()) {
                List<RecommendUser> records = pageInfo.getRecords();

                //试集合长度为0
      /*  for (int i = 0; i < records.size(); i++) {
            records.remove(i);
            i--;
        }
*/

                        RecommendUser recommendUser = new RecommendUser();
                        recommendUser.setUserId(uid);
                        recommendUser.setToUserId(userId);
                        recommendUser.setScore(RandomUtil.randomDouble(80, 99));
                        recommendUserList.add(recommendUser);
                    }
                }

                List<TodayBest> todayBestList = new ArrayList<>();

                //TODO 需要按照条件进行过滤用户，但是需要注意，下面的数据填充的循环要改成UserInfo为外层循环
                //TODO 一定注意推荐数据的顺序，要通过排序方式返回
                //TODO 参考： Collections.sort(todayBestList, (o1, o2) -> Convert.toInt(o2.getFateValue() - o1.getFateValue()));

                //获取到推荐的用户id列表
                List<Object> userIdList = CollUtil.getFieldValues(recommendUserList, "userId");
                //查询用户信息
                List<UserInfo> userInfoList = this.userInfoApi.queryByUserIdList(userIdList);

                for (RecommendUser recommendUser : recommendUserList) {
                    TodayBest todayBest = new TodayBest();
                    todayBest.setFateValue(recommendUser.getScore().longValue());
                    todayBest.setId(recommendUser.getUserId());

                    for (UserInfo userInfo : userInfoList) {
                        if (ObjectUtil.equal(userInfo.getUserId(), recommendUser.getUserId())) {
                            BeanUtil.copyProperties(userInfo, todayBest, "id");
                            // 性别，将枚举的name转成小写
                            todayBest.setGender(userInfo.getSex().name().toLowerCase());
                            break;
                        }
                    }

                    todayBestList.add(todayBest);
                }

                pageResult.setItems(todayBestList);
                return pageResult;
            }
        }

